package Andrew;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * A simple class that opens a server socket, and prints each message received
 * to the console.
 * 
 * @author Graeme Stevenson (graeme.stevenson@ucd.ie)
 */
public class Content implements Runnable {

	/**
	 * Accept this many connections.
	 */
	private final int my_backlog = 5;

	/**
	 * The server socket.
	 */
	private ServerSocket my_serverSocket;

	/**
	 * Create the server socket.
	 * 
	 * @param a_port
	 *            the port that the server socket should listen on.
	 */
	public Content(final int a_port) {
		try {
			my_serverSocket = new ServerSocket(a_port, my_backlog);
			System.out.println("Screen " + a_port);
		} catch (final IOException ioe) {
			ioe.printStackTrace();
		} catch (final SecurityException se) {
			se.printStackTrace();
		}
	}

	/**
	 * Method that listens on the server socket forever and prints each incoming
	 * message to the console.
	 */
	@Override
	public void run() {
		while (true) {
			try {
				// Listens for a connection to be made to this socket.
				final Socket socket = my_serverSocket.accept();

				// Wrap a buffered reader round the socket input stream.
				// Read the javadoc to understand why we do this rather than
				// dealing
				// with reading from raw sockets.
				final BufferedReader in = new BufferedReader(
						new InputStreamReader(socket.getInputStream()));

				// Read in the message
				// I change the values to double for better accuracy when
				// dividing

				// EXERCISE: Instead of printing out client messages to the
				// console:
				// 1. Construct a response in the form
				// "Your message is: <message>".
				// 2. Send the response back to the client.

				final PrintWriter out = new PrintWriter(
						socket.getOutputStream(), true);

				// Write the message to the socket.
				final File dir = new File(
						"C:/Documents and Settings/Andrew/Dist");
				final String filename = "";
				String[] children = null;

				final FileFilter fileFilter = new FileFilter() {
					@Override
					public boolean accept(final File file) {
						return file.isDirectory();
					}
				};
				final File[] files = dir.listFiles(fileFilter);

				for (int i = 0; i < files.length; i++) {
					final FilenameFilter filter = new FilenameFilter() {
						@Override
						public boolean accept(final File dir, final String name) {
							final String regex = "^.*\\.(mp3|avi|mp4|mkv|vob|mov|mp2m4a)$";
							;
							return name.matches(regex);
						}
					};
					children = files[i].list(filter);
					if (children == null) {
						// Either dir does not exist or is not a directory
						System.out.println("Directory no found");
					} else {
						for (int j = 0; j < children.length; j++) {
							// Get filename of file or directory
							out.println(children[j]);
							System.out.println(children[j]);
						}
					}
				}
				// tidy up
				in.close();
				out.close();
				socket.close();
			} catch (final IOException ioe) {
				ioe.printStackTrace();
			} catch (final SecurityException se) {
				se.printStackTrace();
			}
		}
	}

	/**
	 * Parse the arguments to the program and create the server socket.
	 * 
	 * @param args
	 *            the program arguments. Should take the form &lt;port&gt;
	 */
} // end class

